﻿using System;
using Sundstrom.Validation;

namespace Sundstrom.Mvvm.Validation
{
    [AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
    public sealed class NotEmptyAttribute : ValidationAttribute
    {
        public NotEmptyAttribute()
        {
            ErrorMessage = "Must not be null or empty.";
        }

        public override ValidationResult Validate(object value, ValidationContext validationContext)
        {
            var v = value as string;

            if (v != null)
            {
                if (string.IsNullOrEmpty(v) || string.IsNullOrWhiteSpace(v))
                    return new ValidationResult(ErrorMessage, validationContext.MemberName);
            }
            else if (value == null)
            {
                return new ValidationResult(ErrorMessage, validationContext.MemberName);
            }

            return null;
        }
    }
}